import { correctService } from '@/utils/request'

function getOS() {
  var sUserAgent = navigator.userAgent;
  var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
  var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
  if (isMac) return "Mac";
  var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
  if (isUnix) return "Unix";
  var isLinux = (String(navigator.platform).indexOf("Linux") > -1);
  if (isLinux) return "Linux";
  if (isWin) {
    var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1;
    if (isWin2K) return "Win2000";
    var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1;
    if (isWinXP) return "WinXP";
    var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1;
    if (isWin2003) return "Win2003";
    var isWinVista = sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1;
    if (isWinVista) return "WinVista";
    var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1;
    if (isWin7) return "Win7";
    var isWin10 = sUserAgent.indexOf("Windows NT 10") > -1 || sUserAgent.indexOf("Windows 10") > -1;
    if (isWin10) return "Win10";
  }
  return "other";
}

export function check (doc, linebreak = 1) {
  // linebreak 下标中是否需要计算\n的长度（0=计算，1=不算）
  // filterIgnore 是否需要过滤用户忽略的结果 （1=过滤，0=不过滤）
  // combine 是否需要合并下标相同的结果 （0=不合并，1=合并）
  return correctService({
    url: 'correct/api/accessToken',
    method: 'post',
    data: {
      text: doc,
      linebreak,
      filterIgnore: 0,
      combine: 1,
      moduler: []
    }
  })
}

export function checkWithInfo (docId, doc, linebreak, index) {
  // linebreak 下标中是否需要计算\n的长度（0=计算，1=不算）
  // filterIgnore 是否需要过滤用户忽略的结果 （1=过滤，0=不过滤）
  // combine 是否需要合并下标相同的结果 （0=不合并，1=合并）
  let request_info = {
    section_index: index,
    postTime: Date.parse(new Date()) / 1000,
    terminal: "wps",
    system_info: getOS()
  }

  window.alert(JSON.stringify(request_info))
  window.alert(JSON.stringify(window.localStorage.getItem('token').slice(-10)))
  return correctService({
    url: 'correct/api/accessToken',
    method: 'post',
    data: {
      docId: docId,
      text: doc,
      linebreak,
      filterIgnore: 0,
      combine: 1,
      moduler: [],
      isOfficial: true,
      request_info: request_info
    }
  })
}

export function replace (docId, doc, item) {
  return correctService({
    url: 'operate/replace/save',
    method: 'post',
    data: {
      docId: docId, text: doc, correct: item
    }
  })
}

export function cancelReplace (docId, item) {
  return correctService({
    url: 'operate/replace/delete',
    method: 'post',
    data: {
      docId: docId, correct: item
    }
  })
}

export function ingore (docId, doc, item) {
  return correctService({
    url: 'operate/ignore/save',
    method: 'post',
    data: {
      docId: docId, text: doc, correct: item
    }
  })
}
